package queues;

import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.CheckSpeedModel;

import systems.CurrentCheckSpeedProcess;
import crawlers.WebpagetestBreakdown;
import crawlers.WebpagetestCrawler;
import crawlers.WebpagetestResult;
import entities.CheckSpeed;

public class ProcessCheckSpeed extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		String u = (String) request.getParameter("u");
		
		if (name == null || name.isEmpty()) {
			return ;
		}
		
		name = UrlHelper.getMainDomain(name);
		
		if (!UrlHelper.checkDomain(name)) {
			return ;
		}
		
		CheckSpeedModel checkSpeedModel = CheckSpeedModel.getInstance();
		CheckSpeed checkSpeed = checkSpeedModel.get(name);
		
		if (checkSpeed == null) {
			checkSpeed = new CheckSpeed();
			checkSpeed.setProcessing(true);
			checkSpeed.setDomain(name);
		}
		
		if (checkSpeed.getId().isEmpty() && (u == null || u.isEmpty())) {
			//Get domain id
			String id = WebpagetestCrawler.getDomainId(name);
			if (!id.isEmpty()) {
				checkSpeed.setId(id);
				checkSpeedModel.insert(checkSpeed);
			}
			
			try {
				Thread.sleep(60000);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		
		System.out.println("Processing domain " + name + "....");
		
		checkSpeed = getData(checkSpeed);
		
		checkSpeed.setProcessing(false);
		checkSpeedModel.insert(checkSpeed);
		
		System.out.println("Proces done " + name + "....");
		
		CurrentCheckSpeedProcess.remove(name);
		
	}
	
	private CheckSpeed getData(CheckSpeed checkSpeed) {
		WebpagetestResult result = new WebpagetestResult(checkSpeed.getId());
		
		//Get table result
		String tableResult = result.getTableResult();
		checkSpeed.setTableResult(tableResult);
		
		//Get 1waterFallThumb
		String imgWaterfall = result.get1WaterfallThumb(checkSpeed.getId());
		checkSpeed.setWaterfallThumb(imgWaterfall);
		
		//Get waterfallBlobStore
		/*if (!imgWaterfall.isEmpty()) {
			String waterfallBlobStore = "";
			try {
				waterfallBlobStore = FileHelper.saveImageToFile(imgWaterfall);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			checkSpeed.setWaterfallBlobStore(waterfallBlobStore);
		}*/
		
		
		
		//Get image screen
		String imgScreen = result.get1ScreenThumb(checkSpeed.getId());
		checkSpeed.setScreenThumb(imgScreen);
		
		//Get image cachedwaterfall
		String imgCachedWaterfall = result.get1CachedWaterfallThumb(checkSpeed.getId());
		checkSpeed.setWaterfallCachedThumb(imgCachedWaterfall);
		
		//Set waterfallCachedBlobStore
		/*if (!imgCachedWaterfall.isEmpty()) {
			String waterfallCachedBlobStore = "";
			try {
				waterfallCachedBlobStore = FileHelper.saveImageToFile(imgCachedWaterfall);
			} catch (Exception e) {
				// TODO: handle exception
			}
			checkSpeed.setWaterfallCachedBlobStore(waterfallCachedBlobStore);
		}*/
		
		
		//Get image cached screen
		String imgCachedScreen = result.get1CachedScreenThumb(checkSpeed.getId());
		checkSpeed.setScreenCachedThumb(imgCachedScreen);
		
		
		WebpagetestBreakdown breakdown = new WebpagetestBreakdown(checkSpeed.getId());
		
		//Get image connection view
		String imgConnectionView = breakdown.getConnectionView(checkSpeed.getId());
		checkSpeed.setConnectionView(imgConnectionView);
		
		//Set connectionViewBlobStore
		/*if (!imgConnectionView.isEmpty()) {
			String connectionViewBlobStore = "";
			try {
				connectionViewBlobStore = FileHelper.saveImageToFile(imgConnectionView);
			} catch (Exception e) {
				// TODO: handle exception
			}
			checkSpeed.setConnectionViewBlobStore(connectionViewBlobStore);
		}*/
		
		
		//Set  image connection view RP
		String imgConnectionViewRp = breakdown.getConnectionViewRv(checkSpeed.getId());
		checkSpeed.setConnectionViewRp(imgConnectionViewRp);
		
		//Set connection view rp blobstore
		/*if (!imgConnectionViewRp.isEmpty()) {
			String connectionViewRpBlobStore = "";
			try {
				connectionViewRpBlobStore = FileHelper.saveImageToFile(imgConnectionViewRp);
			} catch (Exception e) {
				// TODO: handle exception
			}
			checkSpeed.setConnectionViewRpBlobStore(connectionViewRpBlobStore);
		}*/
		
		
		String string = "<table align=\"center\"><tr><th colspan=\"2\"><h2>Content breakdown by MIME type (First  View)</h2></th></tr><tr><td><div id=\"pieRequestsFv_div\" style=\"width:450px; height:300px;\"></div></td><td><div id=\"pieBytesFv_div\" style=\"width:450px; height:300px;\"></div></td></tr><tr><td><div id=\"tableRequestsFv_div\" style=\"width: 100%;\"></div></td><td><div id=\"tableBytesFv_div\" style=\"width: 100%;\"></div></td></tr></table>";
		string += "<table align=\"center\"><tr><th colspan=\"2\"><h2>Content breakdown by MIME type (Repeat  View)</h2></th></tr><tr><td><div id=\"pieRequestsRv_div\" style=\"width:450px; height:300px;\"></div></td><td><div id=\"pieBytesRv_div\" style=\"width:450px; height:300px;\"></div></td></tr><tr><td><div id=\"tableRequestsRv_div\" style=\"width: 100%;\"></div></td><td><div id=\"tableBytesRv_div\" style=\"width: 100%;\"></div></td></tr></table>";
		String contentBreakdown = breakdown.getContentBreakDown();
		checkSpeed.setContentBreakdown(contentBreakdown);
		
		return checkSpeed;
	}
}
